package net.jjjshop.front.controller.lbt;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.jjjshop.common.entity.lbt.CodePaymentRequest;
import net.jjjshop.common.entity.lbt.LbTransferRecordEntity;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.service.lbt.BuyProductSettlementService;
import net.jjjshop.common.service.lbt.LbTransferRecordService;
import net.jjjshop.common.service.user.UserService;
import net.jjjshop.common.util.SyssetConfConstants;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.log.annotation.OperationLog;
import net.jjjshop.framework.util.LoginUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 用户转账记录表 控制器
 */
@RestController
@RequestMapping("/front/lbtransferrecord")
@Api(tags = "用户转账记录")
public class LbTransferRecordController {
    @Autowired
    private UserService userService;
    @Autowired
    private LbTransferRecordService lbTransferRecordService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private BuyProductSettlementService buyProductSettlementService;



    /**
     * 列表信息
     */
    @ApiOperation(value = "记录查询：type为空，查询全部，1，查询自己转出记录，2，查询自己接收记录")
    @GetMapping("/list")
    public ApiResult<IPage<LbTransferRecordEntity>> list(Integer type,Integer page,Integer limit){
        return ApiResult.ok(lbTransferRecordService.getList(type,page,limit));
    }


    /**
     * 详情数据
     */
    @GetMapping("/info/{id}")
    public ApiResult<LbTransferRecordEntity> info(@PathVariable("id") Integer id){
		LbTransferRecordEntity lbTransferRecord = lbTransferRecordService.getById(id);

        return ApiResult.ok(lbTransferRecord);
    }

    /**
     * 新增数据
     */
    @ApiOperation(value = "权益卡转出")
    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody @Validated LbTransferRecordEntity lbTransferRecord){
        if (lbTransferRecordService.addRecord(lbTransferRecord)) {
            return ApiResult.ok("转出成功");
        }
        return ApiResult.fail("失败");
    }
    /**
     * 新增数据
     */
    @ApiOperation(value = "二维码付款")
    @PostMapping("/addPayment")
    public ApiResult<String> addPayment(@RequestBody @Validated CodePaymentRequest bean){
        if (lbTransferRecordService.addPayment(bean)) {
            return ApiResult.ok("付款成功");
        }
        return ApiResult.fail("付款失败");
    }


    /**
     * 查询权益卡单价数据
     */
    @ApiOperation(value = "查询权益卡单价数据")
    @PostMapping("/queryTzPriceList")
    public ApiResult<?> queryTzPriceList(@RequestParam(value = "limit",defaultValue = "7") Integer limit){
        return ApiResult.ok(lbTransferRecordService.selectTzPriceList(limit));
    }

    /**
     * 测试新增
     */
    @ApiOperation(value = "测试新增")
    @PostMapping("/test")
    public ApiResult<?> test(){

        lbTransferRecordService.calculateTzPrice(new BigDecimal(30000));
        return ApiResult.ok();
    }

    /**
     * 佣金娟献
     */
    @ApiOperation(value = "佣金娟献")
    @PostMapping("/dedicate")
    public ApiResult<String> dedicate(@RequestParam("money") BigDecimal money,@RequestParam("payPass") String payPass){
        Boolean dedicate = lbTransferRecordService.dedicate(money, payPass);
        if (dedicate){
            return ApiResult.ok("捐赠成功！");
        }else{
            return ApiResult.fail("捐赠失败！");
        }
    }

    /**
     * 佣金转账
     */
    @ApiOperation(value = "佣金转账")
    @PostMapping("/amountTransfer")
    public ApiResult<String> amountTransfer(@RequestBody @Validated CodePaymentRequest bean){
        User user = userService.getById(LoginUtil.getUserId());
        String redisKey = SyssetConfConstants.USER_AMOUNTTRANSFER_KEY + user.getUserId();
        long submitCount = redisTemplate.opsForValue().increment(redisKey, 1);
        redisTemplate.expire(redisKey, SyssetConfConstants.USER_AMOUNTTRANSFER_TIME, TimeUnit.SECONDS);
        if (submitCount > 1) {
            return ApiResult.fail("操作频繁,请稍后重试");
        }

        if (StringUtils.isEmpty(bean.getPhone())){
            return ApiResult.fail("接收账户不能为空");
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getMobile,bean.getPhone());
        User byPhone = userService.getOne(wrapper);
        if (null == byPhone){
            return ApiResult.fail("接收账户未找到");
        }
        if (StringUtils.isEmpty(bean.getPayPass())){
            return ApiResult.fail("支付密码不能为空");
        }
//        if (user.getOrderNum() <= 0){
//            return ApiResult.fail("您还未激活账户");
//        }
        bean.setUid(byPhone.getUserId());
        if (lbTransferRecordService.addPayment(bean)) {
            return ApiResult.ok("转账成功");
        }
        return ApiResult.fail("转账失败");
    }


    /**
     * 闪兑权益卡
     */
    @ApiOperation(value = "闪兑权益卡")
    @PostMapping("/sdQyk")
    public ApiResult<?> sdQyk(@RequestParam("num") String num ,@RequestParam("payPass") String payPass){

      boolean a =  lbTransferRecordService.duiHuanQyk(new BigDecimal(num),payPass);
      if (a){
          return ApiResult.ok("闪兑成功");
      }
        return ApiResult.fail("闪兑失败");
    }

    /**
     * 捐赠佣金
     */
    @ApiOperation(value = "捐赠佣金")
    @PostMapping("/donateMoney")
    public ApiResult<?> donateMoney(@RequestParam("num") String num ,@RequestParam("payPass") String payPass){

        boolean a =  lbTransferRecordService.donateMoney(new BigDecimal(num),payPass);
        if (a){
            return ApiResult.ok("捐赠成功");
        }
        return ApiResult.fail("捐赠失败");
    }

    /**
     * 捐赠佣金
     */
    @ApiOperation(value = "捐赠佣金")
    @GetMapping("/getDonateMoney")
    public ApiResult<?> getDonateMoney(){

            return ApiResult.ok(lbTransferRecordService.getDonateMoney());
    }


    @RequestMapping(value = "/getQykInfo", method = RequestMethod.GET)
    @OperationLog(name = "getQykInfo")
    @ApiOperation(value = "getQykInfo", response = String.class)
    public ApiResult<Map<String, Object>> getQykInfo(){
        return ApiResult.ok(lbTransferRecordService.getQykInfo());
    }

    /**
     * 测试权益卡发放
     */
    @ApiOperation(value = "发放权益卡")
    @PostMapping("/ffa")
    public ApiResult<?> ffa(){

       buyProductSettlementService.SensibleSettlement();
            return ApiResult.ok("闪兑成功");
    }

}
